<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="企业名称">
              <a-input placeholder="请输入企业名称" v-model="name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">

            <a-form-item label="联系人">
              <a-input placeholder="请输入联系人" v-model="contactMan"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="8">
          <a-form-item
            label="下次何时联系"
            >
            <a-date-picker showTime format="YYYY-MM-DD" v-model="nextTime" />
          </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <a-form-item label="联系方式">
              <a-input placeholder="请输入联系方式" v-model="telphone"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <a-form-item label="送过小礼品">
              <j-dict-select-tag @change="chang($event,'gift')"  :value="sendGift" :triggerChange="true"
                                  dictCode="xc_yes_no"></j-dict-select-tag>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <a-form-item label="已经合作">
              <j-dict-select-tag @change="chang($event,'teamwork')"  :value="teamwork" :triggerChange="true"
                                 dictCode="xc_yes_no"></j-dict-select-tag>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <a-form-item label="企业意向级别">
              <j-dict-select-tag @change="chang($event,'likeLevel')"  :value="likeLevel" :triggerChange="true"
                                 dictCode="xc_like_level"></j-dict-select-tag>
            </a-form-item>
          </a-col>


          <a-col :md="6" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">刷新</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button v-has="'show:企业管理:导出'" type="primary" icon="download" @click="handleExportXls('企业管理')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!--<a-dropdown v-if="selectedRowKeys.length > 0">-->
        <!--<a-menu slot="overlay">-->
          <!--<a-menu-item key="1" @click="batchDel">-->
            <!--<a-icon type="delete"/>-->
            <!--删除-->
          <!--</a-menu-item>-->
        <!--</a-menu>-->
        <!--<a-button style="margin-left: 8px"> 批量操作-->
          <!--<a-icon type="down"/>-->
        <!--</a-button>-->
      <!--</a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>
        <span>已选择</span>
        <a style="font-weight: 600">
          {{ selectedRowKeys.length }}
        </a>
        <span>项</span>
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>

        <span style="float:right;">
           <a-popover title="自定义列" trigger="click" placement="leftBottom">
          <template slot="content">
            <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
              <a-row>
                <template v-for="(item,index) in defColumns">
                  <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                    <a-col :span="12"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                  </template>
                </template>
              </a-row>
            </a-checkbox-group>
          </template>
          <a><a-icon type="setting" />自定义列</a>
        </a-popover>
        </span>
      </div>

      <a-table
        :scroll="{ x: 1800 }"
        class="table-font"
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type:type}"
        @change="handleTableChange"
        :customRow="clickThenCheck">

         <span slot="remark" slot-scope="text,record">
<!--          <j-ellipsis :value="text"/>-->
            <a @click="handleDetail(record)">查看</a>
        </span>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" v-has="'delete:企业管理'"/>
          <a-popconfirm v-has="'delete:企业管理'" title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <a-tabs defaultActiveKey=1>
      <a-tab-pane tab="跟进记录" :key="refKeys[0]" :forceRender="true">
        <companyAttention-List ref="CompanyAttentionList"></companyAttention-List>
      </a-tab-pane>
    </a-tabs>

    <!-- 表单区域 -->
    <company-modal ref="modalForm" @ok="modalFormOk"/>

  </a-card>
</template>

<script>
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import CompanyModal from './modules/CompanyModal'
  import { deleteAction } from '@/api/manage'
  import CompanyAttentionList from './CompanyAttentionList'
  import CompanyAttentionModal from './modules/CompanyAttentionModal'
  import Vue from 'vue'

  export default {
    name: 'CompanyList',
    mixins: [JeecgListMixin],
    components: {
      CompanyModal,
      CompanyAttentionModal,
      CompanyAttentionList
    },
    data() {
      return {
        nextTime:'',
        refKeys: ['companyAttention'],
        description: '企业管理管理页面',
        // 表头
        //表头
        columns:[

        ],
        //列设置
        settingColumns:[],
        defColumns:[
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '企业名称',
            align: 'center',
            dataIndex: 'name'
          },
          {
            title: '地址',
            align: 'center',
            dataIndex: 'address'
          },
          {
            title: '规模',
            align: 'center',
            dataIndex: 'effect'
          },
          {
            title: '联系人',
            align: 'center',
            dataIndex: 'contactMan'
          },
          {
            title: '职位',
            align: 'center',
            dataIndex: 'jobPosition'
          },
          {
            title: '联系方式',
            align: 'center',
            dataIndex: 'telphone'
          },
          {
            title: '送过小礼品',
            align: 'center',
            dataIndex: 'sendGift_dictText'
          },
          {
            title: '已经合作',
            align: 'center',
            dataIndex: 'teamwork_dictText'
          },
          {
            title: '意向级别',
            align: 'center',
            dataIndex: 'likeLevel_dictText'
          },
          {
            title: '备注',
            align:"center",
            dataIndex: 'remark',
            scopedSlots: { customRender: 'remark' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],

        //操作url
        type: 'radio',
        url: {
          list: '/company/company/list',
          delete: '/company/company/delete',
          deleteBatch: '/company/company/deleteBatch',
          exportXlsUrl: 'company/company/exportXls',
          importExcelUrl: 'company/company/importExcel'
        },
        name: '',
        contactMan: '',
        telphone: '',
        sendGift:'',
        teamwork:'',
        likeLevel:''
      }
    },
    computed: {
      importExcelUrl: function() {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
      }
    },
    created() {
      this.initColumns();
    },
    methods: {
      initColumns() {
        //权限过滤（列权限控制时打开，修改第二个参数为授权码前缀）
        //this.defColumns = colAuthFilter(this.defColumns,'testdemo:');

        var key = this.$route.name + ":colsettings";
        let colSettings = Vue.ls.get(key);
        if (colSettings == null || colSettings == undefined) {
          let allSettingColumns = [];
          this.defColumns.forEach(function(item, i, array) {
            allSettingColumns.push(item.dataIndex);
          })
          this.settingColumns = allSettingColumns;
          this.columns = this.defColumns;
        } else {
          this.settingColumns = colSettings;
          const cols = this.defColumns.filter(item => {
            if (item.key == 'rowIndex' || item.dataIndex == 'action') {
              return true;
            }
            if (colSettings.includes(item.dataIndex)) {
              return true;
            }
            return false;
          })
          this.columns = cols;
        }
      },


      //列设置更改事件
      onColSettingsChange (checkedValues) {
        var key = this.$route.name+":colsettings";
        Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000)
        this.settingColumns = checkedValues;
        const cols = this.defColumns.filter(item => {
          if(item.key =='rowIndex'|| item.dataIndex=='action'){
            return true
          }
          if (this.settingColumns.includes(item.dataIndex)) {
            return true
          }
          return false
        })
        this.columns =  cols;
      },


      chang(value,text){
        if(text==='gift'){
          this.sendGift=value
        }else if(text==='teamwork'){
          this.teamwork=value
        }else if(text==='likeLevel'){
          this.likeLevel=value
        }
      },
      clickThenCheck(record) {
        return {
          on: {
            click: () => {
              this.onSelectChange(record.id.split(','), [record])
            }
          }
        }
      },
      onSelectChange(selectedRowKeys, selectionRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectionRows = selectionRows
        this.$refs.CompanyAttentionList.getMain(this.selectedRowKeys[0])
      },
      onClearSelected() {
        this.selectedRowKeys = []
        this.selectionRows = []
        this.$refs.CompanyAttentionList.queryParam.mainId = null
        this.$refs.CompanyAttentionList.loadData()
        this.$refs.CompanyAttentionList.selectedRowKeys = []
        this.$refs.CompanyAttentionList.selectionRows = []
      },

      handleDelete: function(id) {
        var that = this
        deleteAction(that.url.delete, { id: id }).then((res) => {
          if (res.success) {
            that.$message.success(res.message)

            that.loadData()
            that.$refs.CompanyAttentionList.queryParam.mainId = null
            that.$refs.CompanyAttentionList.loadData()
          } else {
            that.$message.warning(res.message)
          }
        })
      },
      searchQuery: function() {
        this.selectedRowKeys = []
        this.selectionRows = []
        this.$refs.CompanyAttentionList.queryParam.mainId = null
        this.$refs.CompanyAttentionList.loadData()
        this.$refs.CompanyAttentionList.selectedRowKeys = []
        this.$refs.CompanyAttentionList.selectionRows = []

        if(this.name!==''){
          this.queryParam.name = this.name
        }
        if(this.contactMan!==''){
          this.queryParam.contactMan =  this.contactMan
        }

        if(this.telphone!==''){
          this.queryParam.telphone = this.telphone
        }


        if(this.nextTime!==''){
          // var nextTime = this.nextTime?this.nextTime.format('YYYY-MM-DD'):null;

          this.queryParam.nextTime =  this.nextTime?this.nextTime.format('YYYY-MM-DD'):null;
        }

        this.queryParam.sendGift =  this.sendGift
        this.queryParam.teamwork = this.teamwork
        this.queryParam.likeLevel =  this.likeLevel

        this.loadData(1)

        this.clearParams()
      },
      clearParams(){
        this.queryParam.name=""
        this.queryParam.contactMan=""
        this.queryParam.telphone=""
        this.queryParam.sendGift=""
        this.queryParam.teamwork=""
        this.queryParam.likeLevel=""
        this.queryParam.nextTime=""
      },
    }
  }
</script>
<!--<style scoped lang="stylus">-->
  <!--.table-font >>> .ant-table-->
    <!--font-size 20px-->

<!--</style>-->
<style scoped>

  @import '~@assets/less/common.less';



</style>